/*
 ** Database.h
 **
 ** Published / author: 2001-01-04 / grymse@telia.com
 **/
/*
Copyright (C) 2001 Anders Hedstrom

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _DATABASE_H
#define _DATABASE_H

#include <string>
#include <list>

#ifdef ODBCW_NAMESPACE
namespace ODBCW_NAMESPACE {
#endif


class Query;
class IError;
class Session;

class Database
{
	/** Mutex container class, used by Lock.
		\ingroup threading */
public:
	class Mutex {
	public:
		Mutex();
		~Mutex();
		void Lock();
		void Unlock();
	private:
#ifdef _WIN32
		HANDLE m_mutex;
#else
		pthread_mutex_t m_mutex;
#endif
	};
	/** Mutex helper class. */
private:
	class Lock {
	public:
		Lock(Mutex& mutex,bool use);
		~Lock();
	private:
		Mutex& m_mutex;
		bool m_b_use;
	};
public:
	/** Connect with data source name, username and password.
		\param dsn Data source name
		\param u Username
		\param auth Password
		\param p Ptr to log class
	*/
	Database(const std::string& dsn,const std::string& u,const std::string& auth,IError *p = NULL);
	/** Connect with connection string.
		\param driverconnect ODBC driver connection string
		\param p Ptr to log class
	*/
	Database(const std::string& driverconnect,IError *p = NULL);
	/** Connect with mutex for threadsafe connection pool, data source name, username and password.
		\param mutex Threadsafety mutex
		\param dsn Data source name
		\param u Username
		\param auth Password
		\param p Ptr to log class
	*/
	Database(Mutex&,const std::string& dsn,const std::string& u,const std::string& auth,IError *p = NULL);
	/** Connect with mutex for threadsafe connection pool, connection string.
		\param mutex Threadsafety mutex
		\param driverconnect ODBC driver connection string
		\param p Ptr to log class
	*/
	Database(Mutex&,const std::string& driverconnect,IError *p = NULL);
	/** Destructor frees all connections in connection pool, and will warn if
		any of the connections are still busy. */
	virtual ~Database();

	/** Get a free connection. */
	Session *grabdb();
	/** Free a connection in use. */
	void freedb(Session *);
	/** Get query class compatible with this Database implementation. */
	Query *GetQuery();
	/** Get query class compatible with this Database implementation. */
	Query *GetQuery(const std::string& sql);

	short errcode();
	void GetErrorText(char *);

	void SetEscapeChar(char);
	char GetEscapeChar();
	void SetDecimalChar(char);
	char GetDecimalChar();

	IError *GetErrHandler() { return m_errhandler; }
	void error(const std::string& text);

	std::string safestr(const std::string&);
	std::string xmlsafestr(const std::string&);

private:
	short m_driverconnect;
	std::string m_host; // odbc: DSN
	std::string m_user; // odbc: user
	std::string m_password; // odbc: auth
	std::list<Session *> m_sessions;
	short m_errc;
	char m_escape;
	char m_decimal;
	IError *m_errhandler;
	Mutex& m_mutex;
	bool m_b_use_mutex;
};


#ifdef ODBCW_NAMESPACE
} // namespace ODBCW_NAMESPACE {
#endif
#endif // _DATABASE_H
